//
//  WJPublishWordToolbar.m
//  BaiSiBuDeJie
//
//  Created by jerrywang on 2017/10/15.
//  Copyright © 2017年 王军. All rights reserved.
//

#import "WJPublishWordToolbar.h"

@interface WJPublishWordToolbar()

@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewHeight;

@property (nonatomic, strong) NSMutableArray *tagLabelArray;
@property (nonatomic, weak) UIButton *addButton;

@end

@implementation WJPublishWordToolbar

- (NSMutableArray *) tagLabelArray {
    if (!_tagLabelArray) {
        _tagLabelArray = [NSMutableArray array];
    }
    return _tagLabelArray;
}

- (void) awakeFromNib {
    // 加号按钮
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [addButton setImage:[UIImage imageNamed:@"icon_post_add_normal"] forState:UIControlStateNormal];
    [addButton sizeToFit];
    [addButton addTarget:self action:@selector(addButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:addButton];
    self.addButton = addButton;
    
    // 默认传递2个标签
    [self createTagLabels:@[@"吐槽", @"糗事"]];
}

// 创建标签label
- (void)createTagLabels:(NSArray *)tagArray {
    
    // 移除所有的label
    // 让self.tagLabelArray数组中的所有对象执行removeFromSuperview方法
    [self.tagLabelArray makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.tagLabelArray removeAllObjects];
    
    // 所有的标签label
    for (int i = 0; i < tagArray.count; i++) {
        // 创建label
        UILabel *newTagLabel = [[UILabel alloc] init];
        newTagLabel.font = [UIFont systemFontOfSize:14];
        newTagLabel.backgroundColor = WJColor(70, 142, 243);
        newTagLabel.text = tagArray[i];
        newTagLabel.textColor = [UIColor whiteColor];
        newTagLabel.textAlignment = NSTextAlignmentCenter;
        [self.topView addSubview:newTagLabel];
        [self.tagLabelArray addObject:newTagLabel];
        
        // 尺寸
        [newTagLabel sizeToFit];
        newTagLabel.wj_height = WJTagHeight;
        newTagLabel.wj_width += 2 * WJSmallMarin;
    }
    
    // 重新布局子控件
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 所有的标签label
    for (int i = 0; i < self.tagLabelArray.count; i++) {
        // 创建label
        UILabel *newTagLabel = self.tagLabelArray[i];
        
        // 位置
        if (i == 0) {
            newTagLabel.wj_x = 0;
            newTagLabel.wj_y = 0;
        } else {
            // 上一个标签
            UILabel *previousTagLabel = self.tagLabelArray[i - 1];
            CGFloat leftWidth = CGRectGetMaxX(previousTagLabel.frame) + WJSmallMarin;
            CGFloat rightWidth = self.topView.wj_width - leftWidth;
            if (rightWidth >= newTagLabel.wj_width) {
                newTagLabel.wj_x = leftWidth;
                newTagLabel.wj_y = previousTagLabel.wj_y;
            } else {
                newTagLabel.wj_x = 0;
                newTagLabel.wj_y = CGRectGetMaxY(previousTagLabel.frame) + WJSmallMarin;
            }
        }
    }
    
    // 加号按钮
    UILabel *lastTagLabel = self.tagLabelArray.lastObject;
    if (lastTagLabel) {
        CGFloat leftWidth = CGRectGetMaxX(lastTagLabel.frame) + WJSmallMarin;
        CGFloat rightWidth = self.topView.wj_width - leftWidth;
        if (rightWidth >= self.addButton.wj_width) {
            self.addButton.wj_x = leftWidth;
            self.addButton.wj_y = lastTagLabel.wj_y;
        } else {
            self.addButton.wj_x = 0;
            self.addButton.wj_y = CGRectGetMaxY(lastTagLabel.frame) + WJSmallMarin;
        }
    } else {
        self.addButton.wj_x = 0;
        self.addButton.wj_y = 0;
    }
    
    // 计算工具条的高度
    self.topViewHeight.constant = CGRectGetMaxY(self.addButton.frame);
    CGFloat oldHeight = self.wj_height;
    self.wj_height = self.topViewHeight.constant + self.bottomView.wj_height + WJSmallMarin;
    self.wj_y += oldHeight - self.wj_height;
}


- (void)addButtonClick {
    myWeakSelf;
//    XMGAddTagViewController *addTag = [[XMGAddTagViewController alloc] init];
//    addTag.getTagsBlock = ^(NSArray *tags) {
//        [weakSelf createTagLabels:tags];
//    };
//    addTag.tags = [self.tagLabels valueForKeyPath:@"text"];
//    XMGNavigationController *nav = [[XMGNavigationController alloc] initWithRootViewController:addTag];
//    
//    // 拿到"窗口根控制器"曾经modal出来的“发表文字”所在的导航控制器
//    UIViewController *vc = self.window.rootViewController.presentedViewController;
//    [vc presentViewController:nav animated:YES completion:nil];
}


@end
